package com.dailyblue.java.project.bookstore.controller;

import com.dailyblue.java.project.bookstore.bean.User;
import com.dailyblue.java.project.bookstore.service.UserService;
import com.dailyblue.java.project.bookstore.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@CrossOrigin
@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/{page}/{size}")
    public JsonResult find(@PathVariable("page") int page, @PathVariable("size") int size) {
        JsonResult result = userService.find(page, size);
        log.info("获取到第{}页数据,内容是：{}", page, result.getData());
        return result;
    }

    @GetMapping("/{name}/{page}/{size}")
    public JsonResult find(@PathVariable("name") String name, @PathVariable("page") int page, @PathVariable("size") int size) {
        return userService.find(name, page, size);
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable long id) {
        return userService.updatePassword(id);
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable String id) {
        return userService.deleteUser(id);
    }

    @PutMapping("/{id}/{state}")
    public JsonResult disable(@PathVariable("id") String id, @PathVariable("state") int state) {
        return userService.disableUser(id, state);
    }

    @PutMapping
    public JsonResult update(User user) {
        return userService.updateUser(user);
    }

    @PostMapping("/login")
    public JsonResult login(User user) {
        return userService.login(user);
    }
}
